package com.clps.rulesregulations.service.impl;

import com.clps.rulesregulations.dao.DeptDao;
import com.clps.rulesregulations.domain.Dept;
import com.clps.rulesregulations.service.DeptService;
import com.clps.rulesregulations.util.ResourceTree;
import com.clps.rulesregulations.util.TreeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 资源逻辑处理
 *
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDao deptDao;
    @Override
    public PageInfo<Dept> findDeptsByPage(Map<String, Object> condition) {
        Integer pageNum = 1;
        Integer pageSize = 10;
        try {
            pageNum = (Integer) condition.get("pageNum");
            pageNum = pageNum==0?1:pageNum;
        }catch (Exception e){
            pageNum = 1;
        }
        try {
            pageSize = (Integer) condition.get("pageSize");
            pageSize = pageSize==0?10:pageSize;
        }catch (Exception e){
            pageSize = 10;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Dept> depts = deptDao.findDeptsByCondition(condition);
        return new PageInfo<Dept>(depts);
    }

    @Override
    public List<Dept> findAll() {
        Map<String,Object> condition = new HashMap<>();
        return deptDao.findAll();
    }

    @Override
    @Cacheable(cacheNames="depts",key="#map['userId'].toString()+#map['type']")
    public List<Dept> loadUserDepts(Map<String, Object> map) {
        return deptDao.loadUserDepts(map);
    }

    @Override
    public List<Dept> findDeptsByRoleId(Integer roleId) {
        return deptDao.findDeptsByRoleId(roleId);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void addDept(Dept dept) {
        deptDao.addDept(dept);
    }


    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void delDept(Integer deptId) {
        deptDao.delDept(deptId);
    }

    @Override
    public void deleteAll(String[] ArrayDATA_IDS) {
        deptDao.deleteAll(ArrayDATA_IDS);
    }

    @Override
    public Dept findDeptById(Integer deptId) {
        return deptDao.findDeptById(deptId);
    }

    @Override
    public Dept findDeptByUrl(String url) {
        return deptDao.findDeptByUrl(url);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void updateDept(Dept dept) {
        deptDao.updateDept(dept);
    }

    @Override
    public List<ResourceTree> findAllAsTree() {
        LinkedList<ResourceTree> deptTrees = deptDao.findAllAsTree();
        ResourceTree root = new ResourceTree(0,null);
        root = TreeUtil.getTree(root,deptTrees);
        return TreeUtil.createDepthFirstTraversalList(root);
    }

    @Override
    public List<Dept> selectAll() {
        List<Dept> cnAreas = deptDao.findMenus();
        return cnAreas;
    }

//    @Override
//    public List<DeptTree> findMenuTree() {
//        LinkedList<DeptTree> deptTrees = deptDao.findMenuTree();
//        DeptTree root = new DeptTree(0, null);
//        root = TreeUtil.getTree(root, deptTrees);
//        return TreeUtil.createDepthFirstTraversalList(root);
//    }

}
